package ird.demo.controller;
 
import ird.demo.bo.AjaxResult;
import ird.demo.common.DemoConstant;
import ird.demo.common.HotkeyUtil;
import ird.demo.domain.Menu;
import ird.demo.domain.Transaction;
import ird.demo.domain.User;
import ird.demo.service.CommonService;
import ird.demo.service.LdapService;

import java.security.Principal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

 
@Controller
public class LoginController{
	
	protected Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	
	@Autowired
	private CommonService service;
	
	@Autowired
	private LdapService ldapSvc;
	
	@RequestMapping(value="/login")
	public String login(ModelMap model, HttpServletRequest request) {
//		User user = service.findUser("user1");
//		if(user != null){
//			String usrGrp = user.getUsrGrp();
//			System.out.println("user.getUsrGrp()="+usrGrp);
//			List<Menu> menus = service.findMenuByGrp(usrGrp);
//			System.out.println("88888888888888"+menus);
//			for(Menu menu:menus){
//				System.out.println(menu.getMenuId()+","+menu.getMenuDesc()+","+menu.getMenuSuppInd());
//			}
//			List<Transaction> trans = service.findTranInMenuByUsrGrp(usrGrp);
//			System.out.println("7777777777"+trans);
//			
//			for(Transaction tran:trans){
//				System.out.println(tran.getMenuId()+","+tran.getTranId()+","+tran.getTranDesc());
//			}
//		}
//		model.addAttribute("usrId", "howard123");
		System.out.println("login");
		return "sysadm/login";
 
	}
	
	public void clearSession(HttpSession session){
		session.removeAttribute("tranId");
		session.removeAttribute("tranDesc");
		session.removeAttribute("menuId");
		session.removeAttribute("menuDesc");
		session.removeAttribute("paraMap");
	}
	
	@RequestMapping(value="/mainMenu")
	public String mainMenu(ModelMap model, HttpSession session, Principal principal) {
		System.out.println("============mainMenu");
		
		clearSession(session);
		
		String userId = principal.getName();
		System.out.println("userId="+userId);
		
//		session.setAttribute("userId", userId);
//		Date currentDate = new Date();
//		model.addAttribute("currentDate", FormatHelper.formatDate(currentDate));
//		model.addAttribute("currentTime", FormatHelper.formatTime(currentDate));
		model.addAttribute("title", "IRD Web Applications Main Menu");
//		LdapUser ldapUsr = ldapSvc.findByPrimaryKey(userId);
		
		User user = service.findUser(userId);
//		System.out.println("user="+user);
		List<Menu> menuList = service.findMenuByGrp(user.getUsrGrp());
//		System.out.println("menuList="+menuList);
		model.addAttribute("menuList", menuList);
		model.addAttribute("headerBtns",HotkeyUtil.getPrintBtn() + HotkeyUtil.getLogoutBtn());
		return "mainMenu";
	}
	
	@RequestMapping(value="/subMenu")
	public String subnMenu(@RequestParam String menuId,@RequestParam String menuDesc, HttpServletRequest request, HttpSession session, ModelMap model) {
		
		System.out.println("============subMenu, menuId=:"+menuId);
		clearSession(session);
		session.setAttribute("menuId", menuId);
		session.setAttribute("menuDesc", menuDesc);
		
		model.addAttribute("title", menuDesc);
		model.addAttribute("menuId", menuId.trim());
		
		List<Transaction> tranList = service.findTranInByMenuId(menuId);
//		System.out.println("tranList="+tranList);
		model.addAttribute("tranList", tranList);
		
		String goMainMenuBtn = HotkeyUtil.getGoMainMenuBtn(request.getContextPath());
		model.addAttribute("headerBtns",HotkeyUtil.getPrintBtn() + goMainMenuBtn);
		return "subMenu";
	}
	
//	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
//	public String loginfailed(ModelMap model) {
//		System.out.println("loginfailed");
//		model.addAttribute(DemoConstant.ERR_CODE, "login.invalidUsrPwd");
//		return "login";
// 
//	}
//	
//	@RequestMapping(value="/denied", method = RequestMethod.GET)
//	public String denied(ModelMap model,HttpSession httpSession) {
//		System.out.println("denied");
//		model.addAttribute("denied", "true");
//		return "login";
//	}
//	
//	@RequestMapping(value="/logout", method = RequestMethod.GET)
//	public String logout(ModelMap model) {
// 
//		return "login";
// 
//	}
	
	@RequestMapping(value="/validateLogin", method = RequestMethod.POST)
	@ResponseBody
	public String validateLogin(@RequestBody Map<String,String> paraMap){
		System.out.println("==========paraMap="+paraMap);
//		Map<String, String> resultMap = new HashMap<String,String>();
//		String j_username = paraMap.get("j_username");
//		String j_password = paraMap.get("j_password");
//		if(HotkeyUtil.isNullstr(j_username)){
//			resultMap.put(DemoConstant.RESULT, DemoConstant.FAIL);
//			resultMap.put(DemoConstant.ERR_FIELD, "j_username");
//			resultMap.put(DemoConstant.ERR_MSG, "Username is mandatory");
//		}
//		else if(HotkeyUtil.isNullstr(j_password)){
//			resultMap.put(DemoConstant.RESULT, DemoConstant.FAIL);
//			resultMap.put(DemoConstant.ERR_FIELD, "j_password");
//			resultMap.put(DemoConstant.ERR_MSG, "Password is mandatory");
//		}
//		else{
//			resultMap.put(DemoConstant.RESULT, DemoConstant.SUCCESS);
//		}
		
		String[][] fieldErrcodeArr = {
			{"j_username","login.mandatory.username"},
			{"j_password","login.mandatory.password"}
		};
		
		AjaxResult result = service.validateNullStr(fieldErrcodeArr, paraMap);
		System.out.println("validateLogin.result="+result);
		return gson.toJson(result);
	}
	
 
}